
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:Initialize()

	self.Entity:SetName("Tiberium Gas")
	self.Entity:SetModel( "models/items/combine_rifle_ammo01.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(false)
		phys:EnableDrag(false)
		phys:EnableCollisions(false)
	end

	self.PhysObj = self.Entity:GetPhysicsObject()

	self.Entity:SetColor(0,255,0,0)

	local Gas = ents.Create("env_smoketrail")
	Gas:SetAngles( self.Entity:GetAngles()  )
	Gas:SetPos( self.Entity:GetPos() )
	Gas:SetParent( self.Entity )
	Gas:SetKeyValue("startcolor", "0 120 0")
	Gas:SetKeyValue("endcolor", "0 255 0")
	Gas:SetKeyValue("startsize","60")
	Gas:SetKeyValue("endsize","10")
	Gas:SetKeyValue("lifetime", "2")
	Gas:SetKeyValue("opacity", ".3")
	Gas:Spawn()
	Gas:Activate()

	local Radiate = ents.Create( "point_hurt" )
	Radiate:SetName("Tiberium Gas")
	Radiate:SetParent( self.Entity )
	Radiate:SetKeyValue("DamageRadius", "250" )
	Radiate:SetKeyValue("Damage", "3" )
	Radiate:SetKeyValue("DamageDelay", "1" )
	Radiate:SetKeyValue("DamageType", "262144" )
	Radiate:SetPos( self.Entity:GetPos() )
	Radiate:Fire("turnon", "", 0);
	Radiate:Spawn()
	Radiate:Activate()

end

function ENT:PhysicsUpdate()

end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 36

	local ent = ents.Create( "tibgas" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Think()

	self.Entity:SetColor(0,255,0,0)

	

	local BVic = ents.FindInSphere( self.Entity:GetPos(), 200 )
	
	for x, c in pairs(BVic) do
		if (c.TibResistant != true) then
			local breakcase = c.hasdamagecase or false
			if (c:IsVehicle() || c:GetClass() == "gmod_wheel" || c:GetClass() == "prop_physics" || breakcase == true) then
				local  gdmg = math.random(1,20)
				attack = cbt_dealdevhit(c.Entity, gdmg, 0)
				if (attack != nil) then
					if (attack == 2) then
						c.Entity.cbt.health = 1
					end
					if (attack >= 1) then
						if (c.Entity.cbt.armor >= 1) then
							c.Entity.cbt.armor = c.Entity.cbt.armor - 0.1
						end
					end
				end
			end
		end
	end

	--self.Entity:Remove()
end

function ENT:PhysicsCollide( data, physobj )
	
end

function ENT:OnTakeDamage( dmginfo )
	
end

function ENT:Use( activator, caller )

end